package com.sumer.brain.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumer.brain.common.model.R;
import com.sumer.brain.common.utils.EasyExcelUtil;
import com.sumer.brain.sys.model.entity.SysDictData;
import com.sumer.brain.sys.model.req.SysDictDataReq;
import com.sumer.brain.sys.service.SysDictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Tag(name = "字典数据管理")
@RequestMapping("/sysDictData")
public class SysDictDataController {

    private final SysDictDataService sysDictDataService;

    @Operation(summary = "分页查询字典数据列表", description = "分页查询字典数据列表", tags = "查询")
    @PostMapping("page")
    public R<Page<SysDictData>> page(@RequestBody SysDictDataReq sysDictDataReq) {
        Page<SysDictData> page = new Page<>(sysDictDataReq.getPageNum(), sysDictDataReq.getPageSize());
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(StringUtils.hasText(sysDictDataReq.getDictType()), SysDictData::getDictType, sysDictDataReq.getDictType())
                .eq(sysDictDataReq.getStatus() != null, SysDictData::getStatus, sysDictDataReq.getStatus())
                .like(StringUtils.hasText(sysDictDataReq.getDictLabel()), SysDictData::getDictLabel, sysDictDataReq.getDictLabel());
        return R.ok(sysDictDataService.page(page, lambdaQueryWrapper));
    }

    @Operation(summary = "查询字典数据列表", description = "查询字典数据列表", tags = "查询")
    @PostMapping("list")
    public R<List<SysDictData>> list(@RequestBody SysDictDataReq sysDictDataReq) {
        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<SysDictData>()
                .eq(sysDictDataReq.getDictType() != null, SysDictData::getDictType, sysDictDataReq.getDictType());
        return R.ok(sysDictDataService.list(lambdaQueryWrapper));
    }

    @Operation(summary = "根据id查询字典数据信息", description = "根据id查询字典数据信息", tags = "查询")
    @GetMapping("{id}")
    public R<SysDictData> getById(@PathVariable("id") Long id) {
        return R.ok(sysDictDataService.getById(id));
    }

    @Operation(summary = "新增字典数据", description = "新增字典数据", tags = "新增")
    @PostMapping()
    public R<Boolean> add(@RequestBody @Validated SysDictData sysRole) {
        return R.ok(sysDictDataService.save(sysRole));
    }

    @Operation(summary = "编辑字典数据", description = "编辑字典数据", tags = "编辑")
    @PutMapping
    public R<Boolean> edit(@RequestBody SysDictData sysRole) {
        return R.ok(sysDictDataService.updateById(sysRole));
    }

    @Operation(summary = "根据id批量删除字典数据", description = "根据id删除字典数据", tags = "删除")
    @DeleteMapping("{ids}")
    public R<Boolean> deleteById(@PathVariable("ids") String ids) {
        return R.ok(sysDictDataService.delByIds(ids));
    }

    @Operation(summary = "字段数据导出", description = "字段数据导出", tags = "导出")
    @PostMapping("export")
    public void export(HttpServletResponse response, SysDictData dictData) throws IOException {
        List<SysDictData> list = sysDictDataService.list();
        EasyExcelUtil.simpleWrite(response, "xxx", SysDictData.class, list, "aaa");
    }
}
